package com.sxw.web;

import org.springframework.http.server.reactive.ServerHttpRequest;
import reactor.core.publisher.Mono;

public class ReactiveRequestContextHolder {
    
    public static final String CONTEXT_KEY = "SERVER_HTTP_REQUEST";
    
    public static Mono<ServerHttpRequest> getRequest() {
        return Mono.deferContextual(ctx -> 
            Mono.justOrEmpty(ctx.getOrEmpty(CONTEXT_KEY))
        );
    }
    
    // 可选：添加一些便捷方法
    public static Mono<String> getHeader(String headerName) {
        return getRequest()
                .map(request -> request.getHeaders().getFirst(headerName))
                .defaultIfEmpty("");
    }
    
    public static Mono<String> getPath() {
        return getRequest()
                .map(request -> request.getPath().value())
                .defaultIfEmpty("");
    }
    
    public static Mono<String> getMethod() {
        return getRequest()
                .map(request -> request.getMethod().name())
                .defaultIfEmpty("");
    }
}